﻿
using Microsoft.EntityFrameworkCore;
using FreeRedis;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;
using static System.Reflection.Metadata.BlobBuilder;
using Newtonsoft.Json.Linq;
using CQIE.LabAssist.Models;
using System;
using CQIE.LabAssist.PublicHelp;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace CQIE.LabAssist.DataAccesss
{

    public class MangmentService : IMangmentService
    {
        private readonly _DbContext _Db;
    
    
        public MangmentService(_DbContext db)
        {
           
            _Db = db;
        }
        /// <summary>
        /// 获取所有学期
        /// </summary>
        /// <returns></returns>
        public JsonResult getAllSemester()
        {
            var res=_Db.Semesters.ToList();
            return new JsonResult (res);
        }

        /// <summary>
        /// 获取当前学期
        /// </summary>
        /// <returns></returns>
        public JsonResult getCurrentSemester()
        {
            var res = _Db.Semesters.OrderByDescending(s=>s.Name).FirstOrDefault();
            return new JsonResult(res);
        }

        /// <summary>
        /// 学期新增
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool addSemester(string name,bool state)
        {
            Semester semester = _Db.Semesters.FirstOrDefault(s => s.Name == name);
            if (semester == null)
            {
                semester = new Semester { Name = name, State = state };
                _Db.Semesters.Add(semester);
                return _Db.SaveChanges() > 0;
            }
            else
                return false;
        }



        /// <summary>
        /// 学期删除
        /// </summary>
        /// <param name="semesterId"></param>
        /// <returns></returns>
        public bool deleteSemester(int semesterId)
        {
            Semester s = _Db.Semesters.Where(w => w.Id == semesterId).FirstOrDefault();
            _Db.Semesters.Remove(s);
            return _Db.SaveChanges() > 0;
        }

        /// <summary>
        /// 学期更新
        /// </summary>
        /// <param name="semester"></param>
        /// <returns></returns>
        public bool updateSemester([FromBody] Semester semester)
        {
            Semester s = _Db.Semesters.Where(w => w.Id == semester.Id).FirstOrDefault();
            s.Name = semester.Name;
            s.State =semester.State;
            _Db.Semesters.Update(s);
            return _Db.SaveChanges() > 0;
        }


        /// <summary>
        /// 获取所有学院
        /// </summary>
        /// <returns></returns>
        public JsonResult getAllAcademician()
        {
            var res = _Db.Academicians.ToList();
            return new JsonResult(res);
        }
        /// <summary>
        /// 学院新增
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool addAcademician(string name,bool state)
        {
            Academician semester = _Db.Academicians.FirstOrDefault(s => s.Name == name);
            if (semester == null)
            {
                semester = new Academician { Name = name, State = state };
              
                _Db.Academicians.Add(semester);
                return _Db.SaveChanges() > 0;
            }
            else
                return false;
        }


        /// <summary>
        /// 学院删除
        /// </summary>
        /// <param name="semesterId"></param>
        /// <returns></returns>
        public bool deleteAcademician(int semesterId)
        {
            Academician s = _Db.Academicians.Where(w => w.Id == semesterId).FirstOrDefault();
            _Db.Academicians.Remove(s);
            return _Db.SaveChanges() > 0;
        }

        /// <summary>
        /// 学院更新
        /// </summary>
        /// <param name="semester"></param>
        /// <returns></returns>
        public bool updateAcademician([FromBody] Academician semester)
        {
            Academician s = _Db.Academicians.Where(w => w.Id == semester.Id).FirstOrDefault();
            s.Name = semester.Name;
            s.State = semester.State;
            _Db.Academicians.Update(s);
            return _Db.SaveChanges() > 0;
        }

        /// <summary>
        /// 将安全检查记录导出为Excel
        /// </summary>
        /// <returns></returns>
        public string exportExcel(List<PubCheck> pubCheckList, string filePath)
        {
            // 定义wwwroot路径
            string rootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
            // 确保wwwroot目录存在
            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath);
            }
            // 将用户定义的文件名与wwwroot路径结合
            string fullPath = Path.Combine(rootPath, filePath);

            try
            {
                using (var fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
                {
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("Safety Checks");

                    // 创建表头
                    IRow headerRow = sheet.CreateRow(0);
                    headerRow.CreateCell(0).SetCellValue("检查时间");
                    headerRow.CreateCell(1).SetCellValue("当前学期名称");
                    headerRow.CreateCell(2).SetCellValue("管理员名称");
                    headerRow.CreateCell(3).SetCellValue("实验室名称");
                    headerRow.CreateCell(4).SetCellValue("实验室编号");
                    headerRow.CreateCell(5).SetCellValue("安全检查状态");
                    headerRow.CreateCell(6).SetCellValue("门窗是否正常");
                    headerRow.CreateCell(7).SetCellValue("门窗状态备注");
                    headerRow.CreateCell(8).SetCellValue("电器线路状态");
                    headerRow.CreateCell(9).SetCellValue("电器线路备注");
                    headerRow.CreateCell(10).SetCellValue("消防器件有无");
                    headerRow.CreateCell(11).SetCellValue("消防器件有无备注");
                    headerRow.CreateCell(12).SetCellValue("消防器件有效期");
                    headerRow.CreateCell(13).SetCellValue("消防器件有效期备注");
                    headerRow.CreateCell(14).SetCellValue("仪器设备能否使用");
                    headerRow.CreateCell(15).SetCellValue("仪器设备能否使用备注");
                    headerRow.CreateCell(16).SetCellValue("防盗设施是否完好");
                    headerRow.CreateCell(17).SetCellValue("防盗设施是否完好备注");
                    headerRow.CreateCell(18).SetCellValue("警示标志");
                    headerRow.CreateCell(19).SetCellValue("警示标志备注");
                    headerRow.CreateCell(20).SetCellValue("设备仪器其他安全隐患");
                    headerRow.CreateCell(21).SetCellValue("设备仪器其他安全隐患备注");
                    headerRow.CreateCell(22).SetCellValue("物品及清洁");
                    headerRow.CreateCell(23).SetCellValue("物品及清洁备注");
                    headerRow.CreateCell(24).SetCellValue("其他安全隐患");
                    headerRow.CreateCell(25).SetCellValue("其他安全隐患备注");
                    // ... 其他表头字段

                    // 填充数据
                    int rowIndex = 1;
                    foreach (var check in pubCheckList)
                    {
                        IRow row = sheet.CreateRow(rowIndex++);
                        row.CreateCell(0).SetCellValue(check.DateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        row.CreateCell(1).SetCellValue(check.SemesterName);
                        row.CreateCell(2).SetCellValue(check.AdminName);
                        row.CreateCell(3).SetCellValue(check.LabName);
                        row.CreateCell(4).SetCellValue(check.LabNumber);
                        row.CreateCell(5).SetCellValue(check.State ? "正常" : "异常");
                        row.CreateCell(6).SetCellValue(check.Doors_windows ? "正常" : "异常");
                        row.CreateCell(7).SetCellValue(check.Doors_windows_state);
                        row.CreateCell(8).SetCellValue(check.Electrical_wiring ? "正常" : "异常");
                        row.CreateCell(9).SetCellValue(check.Electrical_wiring_state);
                        row.CreateCell(10).SetCellValue(check.Firefighting_facilities ? "有" : "无");
                        row.CreateCell(11).SetCellValue(check.Firefighting_facilities_state);
                        row.CreateCell(12).SetCellValue(check.Firefighting_facilities_validity ? "有效" : "无效");
                        row.CreateCell(13).SetCellValue(check.Firefighting_facilities_validity_state);
                        row.CreateCell(14).SetCellValue(check.Instruments_equipment ? "可用" : "不可用");
                        row.CreateCell(15).SetCellValue(check.Instruments_equipment_state);
                        row.CreateCell(16).SetCellValue(check.Anti_theft_facilities ? "完好" : "损坏");
                        row.CreateCell(17).SetCellValue(check.Anti_theft_facilities_state);
                        row.CreateCell(18).SetCellValue(check.Warning_sign ? "有" : "无");
                        row.CreateCell(19).SetCellValue(check.Warning_sign_state);
                        row.CreateCell(20).SetCellValue(check.IEOther_safety_hazards ? "有" : "无");
                        row.CreateCell(21).SetCellValue(check.IEOther_safety_hazards_state);
                        row.CreateCell(22).SetCellValue(check.Item_hygiene ? "良好" : "不良");
                        row.CreateCell(23).SetCellValue(check.Item_hygiene_state);
                        row.CreateCell(24).SetCellValue(check.Other_safety_hazards ? "有" : "无");
                        row.CreateCell(25).SetCellValue(check.Other_safety_hazards_state);
                        // ... 其他字段的数据
                    }

                    workbook.Write(fs);
                }
            }
            catch (Exception e)
            {
                
            }


            return filePath;
        }
    }

}
